<?php

namespace app\staff\controller;

use app\staff\model\StaffModel;
use app\staff\model\StaffTypeModel;
use think\admin\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Staff extends Controller
{
    /**
     * 当前操作数据库
     * @var string
     */
    private string $table = 'Staff';

    /**
     * 员工管理
     * @auth true
     * @menu true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $this->title = '员工管理';

        $this->_query(StaffModel::class)->like('phone,username')->equal('type')->order('id', 'DESC')->page(true, true);
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {
        $model = new  StaffTypeModel();
        $staffType = $model->where(['is_deleted' => 0])->column("*", 'id');
        $this->staff_type =  $staffType;
    }

    /**
     * @return void
     */
    public function add()
    {
        StaffModel::mForm('form');
    }

    /**
     * @return void
     */
    public function edit()
    {
        StaffModel::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isPost()) {
            // 账号权限绑定处理
            $data['authorize'] = arr2str($data['authorize'] ?? []);
            if (isset($data['id']) && $data['id'] > 0) {
                unset($data['username']);
            } else {
                // 检查账号是否重复
                if (empty($data['username'])) {
                    $this->error('登录账号不能为空！');
                }
                $map = ['username' => $data['username'], 'is_deleted' => 0];
                if (StaffModel::mk()->where($map)->count() > 0) {
                    $this->error("账号已经存在，请使用其它账号！");
                }
                // 新添加的用户密码与账号相同
                $data['password'] = hashEncryption($data['username']);
            }
        } else {
            // 权限绑定处理
            $data['authorize'] = str2arr($data['authorize'] ?? '');
            // 用户身份数据
            $this->bases = StaffTypeModel::items();
            // 用户权限管理
            //$this->authorizes = StaffTypeModel::items();
        }
    }

    /**
     * 修改用户密码
     * @auth true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function pass()
    {
        $this->_applyFormToken();
        if ($this->request->isGet()) {
            $this->verify = false;
            StaffModel::mForm('pass');
        } else {
            $data = $this->_vali([
                'id.require' => '用户ID不能为空！',
                'password.require' => '登录密码不能为空！',
                'repassword.require' => '重复密码不能为空！',
                'repassword.confirm:password' => '两次输入的密码不一致！',
            ]);
            $user = StaffModel::mk()->find($data['id']);
            if (!empty($user) && $user->save(['password' => hashEncryption($data['password'])])) {
                sysoplog('系统用户管理', "修改用户[{$data['id']}]密码成功");
                $this->success('密码修改成功，请使用新密码登录！', '');
            } else {
                $this->error('密码修改失败，请稍候再试！');
            }
        }
    }

    /**
     * 删除系统用户
     * @auth true
     */
    public function remove()
    {
        StaffModel::mDelete();
    }
}